import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)
const routers = new Router({
  routes: [
    {
      path: '/',
      redirect: '/martin/auth'
    },
    {
      path: '/login',
      name: 'MartinLogin',
      component: () => import('@/views/Login/Login')
    },
    {
      path: '/martin',
      name: 'MartinLayout',
      meta: {
        access: true
      },
      component: () => import('@/views/Layout/Layout'),
      children: [
        {
          path: 'setting',
          name: 'MartinSetting',
          meta: {
            access: true
          },
          component: () => import('@/views/Setting/Setting')
        },
        {
          path: 'auth',
          name: 'MartinAuth',
          meta: {
            access: true
          },
          component: () => import('@/views/Auth/Auth')
        },
        {
          path: 'write/article',
          name: 'MartinWriteArticle',
          meta: {
            access: true
          },
          component: () => import('@/views/WriteArticle/WriteArticle')
        },
        {
          path: 'article/list',
          name: 'MartinArticleList',
          meta: {
            access: true
          },
          component: () => import('@/views/ArticleList/ArticleList')
        },
        {
          path: 'article/detail/:id',
          name: 'MartinArticleDetail',
          meta: {
            access: true
          },
          component: () => import('@/views/ArticleDetail/ArticleDetail')
        },
      ]
    }
  ]
})

routers.beforeEach((to, from, next) => {
  if (!window.localStorage.access_token && to.meta.access) {
    window.localStorage.removeItem('nickname')
    new Vue().$message.error('登录失效')
    routers.push({name: 'MartinLogin'})
  }
  next()
})

export default routers
